package org.swxjava.assembler.avm1;

import org.swxjava.assembler.Constants;
import org.swxjava.assembler.ObjectCompiler;
import org.swxjava.util.ArrayUtils;
import org.swxjava.util.Log;
import org.swxjava.util.LogFactory;
import org.swxjava.util.StringUtils;

public class AsFloat implements ObjectCompiler {

	/** Logger */
	private static final Log LOG = LogFactory.getLog(AsFloat.class);
	
	/** value */
	private Float value = 0.0f;

	public AsFloat(Float value) {
		this.value = value;
	}

	public int getByteSize() {
		return toString().length() / 2;
	}

	public byte[] toBytecode() {
		return null;
	}

	public String toString() {
		return floatToBytecode(value);
	}

	public static String floatToBytecode(float f) {
		if(Float.isNaN(f)) {
			return Constants.NUMBER_NAN;
		}
		String b = Integer.toHexString(Float.floatToRawIntBits(f)).toUpperCase();
		String[] dbytes = StringUtils.split(b, 2);
		
		ArrayUtils.reverse(dbytes);
		
		StringBuffer hex = new StringBuffer();
		for(int i=0; i < 4; i++) {
			hex.append(dbytes[i]);
		}
		String bytecode = Constants.DATA_TYPE_FLOAT + hex.toString();
		if(LOG.isDebugEnabled()) {
			LOG.debug(f + " : " + bytecode);
		}
		return bytecode;
	}
}
